VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsMsgID"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Public Function GetNextKtMessageId(ByRef strNextVal As String) As Boolean

Dim adoConn As ADODB.Connection
Dim rsSeq As ADODB.Recordset
Dim cmdAdo As ADODB.Command
Dim strSql As String
Dim lngRecordsAffected As Long
Dim strFillString As String
Dim strSeqNo As String


On Error GoTo PROC_ERROR

Set adoConn = New ADODB.Connection
With adoConn
    .ConnectionString = GetConnectionString() 'Johann (20090625): Centralised code for connection string added
    .Open
End With

'initilize the recordset
Set rsSeq = New ADODB.Recordset
With rsSeq
    .CursorLocation = adUseClient
    .CursorType = adOpenStatic
End With

strSql = "Select LAST_VALUE from DIR_SEQUENCES where SEQ_NAME = 'DIR_KT_MSG_ID'"
Set rsSeq = adoConn.Execute(strSql)
If rsSeq.RecordCount <> 0 Then
    strSeqNo = rsSeq!LAST_VALUE

    If Not IsNull(rsSeq!LAST_VALUE) Then
        If rsSeq!LAST_VALUE = "999999" Then
            strNextVal = "000001"
        ElseIf rsSeq!LAST_VALUE = "" Then
            MsgBox "DIR_KT_MSG_ID not set up in DIR_SEQUENCES"
            GetNextKtMessageId = False
        Else
            If strSeqNo = 0 Then
                strSeqNo = 1
            Else
                strSeqNo = CLng(strSeqNo) + 1
            End If
            strFillString = String(6, "0")
            'strNextVal = CStr(rsSeq!LAST_VALUE + 1)
            strNextVal = Format(CStr(strSeqNo), strFillString)

            strSql = "Update DIR_SEQUENCES set LAST_VALUE = " & CLng(strNextVal) & " where SEQ_NAME = 'DIR_KT_MSG_ID'"
            Set cmdAdo = New ADODB.Command
            With cmdAdo
                .CommandText = strSql
                .CommandType = adCmdText
                Set .ActiveConnection = adoConn
                .Execute lngRecordsAffected
            End With
            If lngRecordsAffected = 1 Then
                GetNextKtMessageId = True
            Else
                MsgBox "Couldn't update DIR_KT_MSG_ID not set up in DIR_SEQUENCES"
                GetNextKtMessageId = False
            End If

        End If
    Else
        strNextVal = "000001"
    End If
Else
    strNextVal = "000001"
End If

strNextVal = "KT" & strNextVal

'clean up
rsSeq.Close
Set rsSeq = Nothing
adoConn.Close
Set adoConn = Nothing
Set cmdAdo = Nothing

PROC_EXIT:
  Exit Function

PROC_ERROR:
    Err.Raise Err.Number, _
            "clsMsgID/GetNextKtMessageId", _
            Err.Description
  Resume PROC_EXIT
End Function


